CPython -> Python implmentation in C
Python program is read by a parser, input to the parser is a stream of tokens, generated by lexical analyzer.
Two or more physical lines can be interpretted as logical lines by using backslash().
Lines in parenthesis can be split without backslahes
In [4]:
def \
quicksort():
pass
Everything is represented by objects in python or relation among objects. Every object has a value, type, identity. Identity can be thought of as address of object in memory which never changes. The 'is' operator compares the identity of the objects. Type of an object is also unchangable.
List of types available in python
User definded functions
Class Instances A class instance has a namespace implemented as a dictionary where attribute references are first searched. When not found than instance's class has also a atrribute by that name. If not found than self is checked and if still not found than __getattr__() is checked. Attribute assignments and deletions always update the instance's dictionary.<\p>
Various internal types used internally by the interpreter
Special method names
By default instances of classes have a dictioanry for attribute storage. This space consumption can become large when a large number of instances of a class are created. This default can be overridden by using __slots__ in a class definition. It reserves sapce for the declared vaariabels and prevents automatic creation of dict of each instance.
If you want to dynamically declare new vairbales then add __dict__ to the sequence of strings in __slots__ declaration.
Metaclasses
The class creation process can be customized by passing the metaclass keyword argument in the class definition line or by inheriting from an existing class that included such ar argument.
In [1]:
class Meta(type):
pass
class MyClass(metaclass = Meta):
pass
class MySubclass(MyClass):
pass
Python programs are executed as code blocks which are module, function body, class definition. A code block is executed as a execution frame.
To rasie an exception use raise or try --except. The finally clasue of such a staement can be used to specify cleanup code which does not handle the exception but is executed whether an exception occured or not in the preceding code.